var service = require('../../../utils/utilService.js')

let video = null
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      valeu: null,
      observer: function (newValue, oldValue) {
        var isLike = newValue.isAdmire > 0 ? true : false
        var isFav = newValue.isStore > 0 ? true : false
        var nick = newValue.publishUserName != null ? newValue.publishUserName : newValue.fromName
        var location = newValue.publishUserName != null ? newValue.publishUserRegion : newValue.city
        var storyId = newValue.id != null ? newValue.id : newValue.storyId 
        var imageUrl = ''
        var videoUrl = ''

        if (newValue.urls != null && newValue.urls.length > 0) {
          videoUrl = (newValue.urls[0].video != null && newValue.urls[0].video.length > 0) ? newValue.urls[0].video :  ""
          imageUrl = (newValue.urls[0].img != null && newValue.urls[0].img.length > 0) ? newValue.urls[0].img : service.setSwiperData(videoUrl)
        } else if (newValue.storyUrls != null ) {
          if (newValue.storyUrls.length > 0) {
            imageUrl = newValue.storyUrls[0]
          }
          if (newValue.storyUrls.length > 1) {
            videoUrl = newValue.storyUrls[1]
          }
        }

        this.setData({
          isFav: isFav, isLike: isLike,
          favCount: newValue.storeCount,
          likeCount: newValue.admireCount,
          nick: nick,
          location: location,
          imageUrl: service.configCover(imageUrl),
          videoUrl: videoUrl,
          storyId: storyId
        })
      },
    },
    url: {
      type: String,
      value: ""
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isFav: false,
    isLike: false,
    favCount: 0,
    likeCount: 0,
    nick: '',
    location: '',
    imageUrl: '',
    videoUrl: '',
    storyId: ''
  },

  ready: function () {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    likeActionBtnClick() {
      this.triggerEvent("likeCallBack", this.properties.data)
      if (this.data.isLike == false) {
        var that = this
        wx.showLoading({
          title: '请稍等',
        })
        service.likeRequest(this.data.storyId,
          function (data) {
            wx.hideLoading()
            wx.showToast({
              title: '已点赞',
              icon: 'success',
              duration: 2000
            })

            var likeCount = that.data.likeCount + 1
            that.setData({ isLike: true, likeCount: likeCount })

            that.properties.data.isAdmire = 1
            that.triggerEvent("likeCallBack", that.properties.data)
          },
          function (error) {
            wx.hideLoading()
            wx.showToast({
              title: error.message,
              icon: 'none',
              duration: 2000
            })
          })
      }
    },
    favActionBtnClick() {
      if (this.data.isFav == false) {
        this.favRequest()
      } else {
        var that = this
        wx.showModal({
          title: '提示',
          content: '您将取消对此商品的收藏！',
          success: function (res) {
            if (res.confirm) {
              that.deleteFavRequest()
            }
          }
        })
      }
    },

    favRequest() {
      var that = this
      wx.showLoading({
        title: '请稍等',
      })
      service.favRequest(this.data.storyId, 1,
        function (data) {
          wx.hideLoading()
          wx.showToast({
            title: '已收藏',
            icon: 'success',
            duration: 2000
          })

          var favCount = that.data.favCount + 1
          that.setData({ isFav: true, favCount: favCount })

          that.properties.data.isStore = 1
          that.triggerEvent("favCallBack", that.properties.data)
        },
        function (error) {
          wx.hideLoading()
          wx.showToast({
            title: error.message,
            icon: 'none',
            duration: 2000
          })
        })
    },
    deleteFavRequest() {
      var that = this
      wx.showLoading({
        title: '请稍等',
      })
      service.deleteFavRequest(this.data.storyId,
        function (data) {
          wx.hideLoading()
          wx.showToast({
            title: '已取消',
            icon: 'success',
            duration: 2000
          })

          var favCount = that.data.favCount - 1
          that.setData({ isFav: false, favCount: favCount })

          that.properties.data.isStore = 0
          that.triggerEvent("favCallBack", that.properties.data)
        },
        function (error) {
          wx.hideLoading()
          wx.showToast({
            title: error.message,
            icon: 'none',
            duration: 2000
          })
        })
    },
    iconClick() {
      wx.navigateTo({
        url: '/mineSubs/pages/homepage/personalhomepage?userId=' + this.properties.data.fromId,
      })
    },
    showVideo() {
      if (this.data.videoUrl.length > 0) {
        this.triggerEvent("showVideo", this.data.videoUrl)
      } else {
        wx.showToast({
          title: '视频加载错误',
          icon: 'none',
          duration: 2000
        })
      }
    },
    showInfo() {
      wx.navigateTo({
        url: "/goods/pages/goodsDetails/goodsDetails?goods_id=" + this.data.storyId + '&goods_type=9' 
      })
    }
  }
})
